package com.cyjt.operation.ui;

import java.util.ArrayList;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.ViewPagerAdaper;
import com.cyjt.operation.fragment.BaseStationMessageFragment;
import com.cyjt.operation.fragment.OperationMessageFragment;
import com.cyjt.operation.fragment.WorkOrderMessageFragment;
import com.cyjt.operation.view.ActionBarLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 消息管理的Activity
 * 
 * @author LTP
 *
 */
public class MessageManagementActivity extends FragmentActivity {
	/** ViewPager的Tab标题 */
	private Button button_workorder_message, button_operation_message, button_basestation_message;
	/** ViewPager控件 */
	private ViewPager viewpager_message_management;
	/** ViewPager下的Fragment的页面集合 */
	private List<Fragment> fragmentList = new ArrayList<Fragment>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_management);
		initView();
		initViewPager();
	}

	/**
	 * 控件初始化
	 */
	private void initView() {
		ActionBarLayout.setTitle("消息管理");
		WorkOrderMessageFragment mWorkOrderMessageFragment = new WorkOrderMessageFragment();
		OperationMessageFragment mOperationMessageFragment = new OperationMessageFragment();
		BaseStationMessageFragment mBaseStationMessageFragment = new BaseStationMessageFragment();
		fragmentList.add(mWorkOrderMessageFragment);
		fragmentList.add(mOperationMessageFragment);
		fragmentList.add(mBaseStationMessageFragment);
		button_workorder_message = (Button) findViewById(R.id.button_workorder_message);
		button_workorder_message.setOnClickListener(new MyOnclickListener(0));
		button_operation_message = (Button) findViewById(R.id.button_operation_message);
		button_operation_message.setOnClickListener(new MyOnclickListener(1));
		button_basestation_message = (Button) findViewById(R.id.button_basestation_message);
		button_basestation_message.setOnClickListener(new MyOnclickListener(2));
	}

	/**
	 * ViewPager的初始化
	 */
	private void initViewPager() {
		viewpager_message_management = (ViewPager) findViewById(R.id.viewpager_message_management);
		ViewPagerAdaper mViewPagerAdaper = new ViewPagerAdaper(getSupportFragmentManager(), fragmentList);
		viewpager_message_management.setAdapter(mViewPagerAdaper);
		viewpager_message_management.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					button_workorder_message.setBackgroundResource(R.drawable.collect_tab_left_btn_sel);
					button_workorder_message.setTextColor(getResources().getColor(R.color.white));
					button_operation_message.setBackgroundResource(R.drawable.collect_tab_center_btn);
					button_operation_message.setTextColor(getResources().getColor(R.color.black));
					button_basestation_message.setBackgroundResource(R.drawable.collect_tab_right_btn);
					button_basestation_message.setTextColor(getResources().getColor(R.color.black));
					break;
				case 1:
					button_workorder_message.setBackgroundResource(R.drawable.collect_tab_left_btn);
					button_workorder_message.setTextColor(getResources().getColor(R.color.black));
					button_operation_message.setBackgroundResource(R.drawable.collect_tab_center_btn_sel);
					button_operation_message.setTextColor(getResources().getColor(R.color.white));
					button_basestation_message.setBackgroundResource(R.drawable.collect_tab_right_btn);
					button_basestation_message.setTextColor(getResources().getColor(R.color.black));

					break;
				case 2:
					button_workorder_message.setBackgroundResource(R.drawable.collect_tab_left_btn);
					button_workorder_message.setTextColor(getResources().getColor(R.color.black));
					button_operation_message.setBackgroundResource(R.drawable.collect_tab_center_btn);
					button_operation_message.setTextColor(getResources().getColor(R.color.black));
					button_basestation_message.setBackgroundResource(R.drawable.collect_tab_right_btn_sel);
					button_basestation_message.setTextColor(getResources().getColor(R.color.white));

					break;

				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 设置ViewPager自定义标题文本的点击事件
	 * 
	 * @author LTP
	 *
	 */
	private class MyOnclickListener implements OnClickListener {
		private int index;

		public MyOnclickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			viewpager_message_management.setCurrentItem(index);
		}

	}
}
